# Maintainer: Biswapriyo Nath <nathbappai@gmail.com>

_realname=pulseaudio
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=15.0
pkgrel=1
pkgdesc="A featureful, general-purpose sound server (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url="https://www.freedesktop.org/wiki/Software/PulseAudio/"
license=("GPL")
depends=("${MINGW_PACKAGE_PREFIX}-fftw"
         "${MINGW_PACKAGE_PREFIX}-gdbm"
         "${MINGW_PACKAGE_PREFIX}-gettext"
         "${MINGW_PACKAGE_PREFIX}-glib2"
         "${MINGW_PACKAGE_PREFIX}-libiconv"
         "${MINGW_PACKAGE_PREFIX}-libsndfile"
         "${MINGW_PACKAGE_PREFIX}-libsoxr"
         "${MINGW_PACKAGE_PREFIX}-libssp"
         "${MINGW_PACKAGE_PREFIX}-libtool"
         "${MINGW_PACKAGE_PREFIX}-libwinpthread-git"
         "${MINGW_PACKAGE_PREFIX}-orc"
         "${MINGW_PACKAGE_PREFIX}-pcre"
         "${MINGW_PACKAGE_PREFIX}-speexdsp")
makedepends=("${MINGW_PACKAGE_PREFIX}-check"
             "${MINGW_PACKAGE_PREFIX}-doxygen"
             "${MINGW_PACKAGE_PREFIX}-libxml2"
             "${MINGW_PACKAGE_PREFIX}-meson"
             "${MINGW_PACKAGE_PREFIX}-pkg-config")
options=('strip' '!debug' 'staticlibs')
source=("https://freedesktop.org/software/pulseaudio/releases/pulseaudio-${pkgver}.tar.xz"
        "0001-remove-assert-pid_t-mismatch.patch"
        "0002-meson-check-version-script.patch"
        "0003-Fix-pointer-to-integer-cast-warnings.patch")
sha256sums=('a40b887a3ba98cc26976eb11bdb6613988f145b19024d1b6555c6a03c9cba1a0'
            'fa00d79f0cf18999cfee2b7e479319ac819f09a46a00ce8b3906b1c8c417416a'
            'a8a4ee1cf108583b61b499669cb641c00644de680838f45a6e58206a18b68af8'
            '3d5d66d8ce6f53429166a92f6f069f5c6a63061385da219e23300159c76584f9')

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"
  patch -p1 -i "${srcdir}/0001-remove-assert-pid_t-mismatch.patch"
  patch -p1 -i "${srcdir}/0002-meson-check-version-script.patch"

  # https://gitlab.freedesktop.org/pulseaudio/pulseaudio/-/merge_requests/630
  patch -p1 -i "${srcdir}/0003-Fix-pointer-to-integer-cast-warnings.patch"
}

build() {
  [[ -d "${srcdir}/build-${MSYSTEM}" ]] && rm -rf "${srcdir}/build-${MSYSTEM}"
  mkdir "${srcdir}/build-${MSYSTEM}" && cd "${srcdir}/build-${MSYSTEM}"

  declare -a _extra_config
  if check_option "debug" "n"; then
    _extra_config+=("--buildtype=release")
  else
    _extra_config+=("--buildtype=debug")
  fi

  # _WIN32_WINNT < 0x0600 is checked before inet_ntop

  CPPFLAGS+=" -D_WIN32_WINNT=0x0603" \
  MSYS2_ARG_CONV_EXCL="--prefix=;-Dmodlibexecdir=" \
  ${MINGW_PREFIX}/bin/meson.exe \
    --prefix="${MINGW_PREFIX}" \
    -Dmodlibexecdir="${MINGW_PREFIX}/bin" \
    --wrap-mode=nodownload \
    --default-library=both \
    "${_extra_config[@]}" \
    -Ddbus=disabled \
    -Ddatabase=gdbm \
    -Dtests=false \
    -Dwebrtc-aec=disabled \
    "../${_realname}-${pkgver}"

  ${MINGW_PREFIX}/bin/meson.exe compile
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/meson.exe install

  # Workaround: Move import libraies to libdir
  mv "${pkgdir}"${MINGW_PREFIX}/bin/*.a "${pkgdir}"${MINGW_PREFIX}/lib
}
